
 <!DOCTYPE HTML>
<html >
<head>
  <meta charset="UTF-8">
  
    <title>jdk源码分析之List--使用中的坑 | Typhoon&#39;s code space</title>
    <meta name="viewport" content="width=device-width, initial-scale=1,user-scalable=no">
    
    <meta name="author" content="typhoon">
    

    
    <meta name="description" content="之前讲解了一篇  jdk源码分析之List--常用实现类分析与对比scorpioaeolus.gitee.io/typhoon/2018/04/21/list%E5%88%86%E6%9E%90">
<meta name="keywords" content="list使用中的坑">
<meta property="og:type" content="article">
<meta property="og:title" content="jdk源码分析之List--使用中的坑">
<meta property="og:url" content="http://scorpioaeolus.oschina.io/2018/05/26/list使用中的坑/index.html">
<meta property="og:site_name" content="Typhoon&#39;s code space">
<meta property="og:description" content="之前讲解了一篇  jdk源码分析之List--常用实现类分析与对比scorpioaeolus.gitee.io/typhoon/2018/04/21/list%E5%88%86%E6%9E%90">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/26/list使用中的坑/l1.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/26/list使用中的坑/l2.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/26/list使用中的坑/l3.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/26/list使用中的坑/l4.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/26/list使用中的坑/l5.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/26/list使用中的坑/l6.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/26/list使用中的坑/l7.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/26/list使用中的坑/l8.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/26/list使用中的坑/h4.jpg">
<meta property="og:updated_time" content="2018-05-26T10:50:55.318Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="jdk源码分析之List--使用中的坑">
<meta name="twitter:description" content="之前讲解了一篇  jdk源码分析之List--常用实现类分析与对比scorpioaeolus.gitee.io/typhoon/2018/04/21/list%E5%88%86%E6%9E%90">
<meta name="twitter:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/26/list使用中的坑/l1.png">

    
    <link rel="alternative" href="/atom.xml" title="Typhoon&#39;s code space" type="application/atom+xml">
    
    
    <link rel="icon" href="/typhoon/img/java.png">
    
    
    <link rel="apple-touch-icon" href="/typhoon/img/jacman.jpg">
    <link rel="apple-touch-icon-precomposed" href="/typhoon/img/jacman.jpg">
    
    <link rel="stylesheet" href="/typhoon/css/style.css">
</head>

  <body>
    <header>
      
<div>
		
			<div id="imglogo">
				<a href="/typhoon/"><img src="/typhoon/img/java.png" alt="Typhoon&#39;s code space" title="Typhoon&#39;s code space"/></a>
			</div>
			
			<div id="textlogo">
				<h1 class="site-name"><a href="/typhoon/" title="Typhoon&#39;s code space">Typhoon&#39;s code space</a></h1>
				<h2 class="blog-motto"></h2>
			</div>
			<div class="navbar"><a class="navbutton navmobile" href="#" title="Menu">
			</a></div>
			<nav class="animated">
				<ul>
					<ul>
					 
						<li><a href="/typhoon/">主页</a></li>
					
						<li><a href="/typhoon/archives">归档</a></li>
					
						<li><a href="/typhoon/about">关于</a></li>
					
					<li>
 					
					<form class="search" action="//google.com/search" method="get" accept-charset="utf-8">
						<label>Search</label>
						<input type="search" id="search" name="q" autocomplete="off" maxlength="20" placeholder="Search" />
						<input type="hidden" name="q" value="site:scorpioaeolus.oschina.io">
					</form>
					
					</li>
				</ul>
			</nav>			
</div>
    </header>
    <div id="container">
      <div id="main" class="post" itemscope itemprop="blogPost">
  
	<article itemprop="articleBody"> 
		<header class="article-info clearfix">
  <h1 itemprop="name">
    
      <a href="/typhoon/2018/05/26/list使用中的坑/" title="jdk源码分析之List--使用中的坑" itemprop="url">jdk源码分析之List--使用中的坑</a>
  </h1>
  <p class="article-author">By
       
		<a href="/typhoon/about" title="typhoon" target="_blank" itemprop="author">typhoon</a>
		
  <p class="article-time">
    <time datetime="2018-05-26T08:17:16.000Z" itemprop="datePublished"> Published 2018-05-26</time>
    
  </p>
</header>
	<div class="article-content">
		
		<div id="toc" class="toc-article">
			<strong class="toc-title">Contents</strong>
		
			<ol class="toc"><li class="toc-item toc-level-6"><a class="toc-link" href="#循环remove"><span class="toc-number">1.</span> <span class="toc-text">循环remove</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#Arrays-asList-移除元素"><span class="toc-number">2.</span> <span class="toc-text">Arrays.asList()移除元素</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#subList子列表问题"><span class="toc-number">3.</span> <span class="toc-text">subList子列表问题</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#总结"><span class="toc-number">4.</span> <span class="toc-text">总结</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#附上本人公众号"><span class="toc-number">5.</span> <span class="toc-text">附上本人公众号</span></a></li></ol>
		
		</div>
		
		<pre><code>之前讲解了一篇 
</code></pre><blockquote><footer><strong>jdk源码分析之List--常用实现类分析与对比</strong><cite><a href="http://scorpioaeolus.gitee.io/typhoon/2018/04/21/list%E5%88%86%E6%9E%90/" target="_blank" rel="external">scorpioaeolus.gitee.io/typhoon/2018/04/21/list%E5%88%86%E6%9E%90</a></cite></footer></blockquote>
<a id="more"></a>
<pre><code>讲述了常用List实现类以及使用方式和性能对比，那么此篇文章针对List使用过程中遇到的一些坑做一些总结和分析。
</code></pre><h6 id="循环remove"><a href="#循环remove" class="headerlink" title="循环remove"></a>循环remove</h6><pre><code>有下边一段程序:
</code></pre><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line"> public static void main(String[] args) &#123;</div><div class="line"></div><div class="line">            List&lt;Integer&gt; list = new ArrayList&lt;&gt;(10);</div><div class="line">            for(int i = 0; i &lt; 10; i ++) &#123;</div><div class="line">                list.add(i + 1);</div><div class="line">            &#125;</div><div class="line">         System.out.println(&quot;remove之前:&quot; + JSON.toJSONString(list));</div><div class="line"></div><div class="line">            for(int i = 0;i &lt; list.size();i ++) &#123;</div><div class="line">                if(list.get(i) &gt; 2) &#123;</div><div class="line">                    list.remove(i);</div><div class="line">                &#125;</div><div class="line">            &#125;</div><div class="line">        System.out.println(&quot;剩余元素:&quot; + JSON.toJSONString(list));</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>运行结果应该如何呢？
我们期望结果是剩余元素打印:1
运行程序看一下:
</code></pre><img src="/typhoon/2018/05/26/list使用中的坑/l1.png" alt="list" title="list">
<pre><code>从运行结果中可以看到，程序并没有得到我们期望的结果，为什么？
先不做解释，看下图：
</code></pre><img src="/typhoon/2018/05/26/list使用中的坑/l2.png" alt="list" title="list">
<pre><code>    图中我们演示了循环删除元素的执行步骤，导致程序运行结果和我们预期不一致的原因，有一个很关键的点，就是for循环中i &lt; list.size(),
因为随着我们移除元素，list.size()是逐渐变小的，导致循环过程中i的左边界是递增的，右边界是递减的，出现了元素删不掉，也就是上图中的
效果。
那么如果提前把list.size()存储起来再执行for循环移除元素，效果会如何呢？我们把上边的代码稍微做一下改动：
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</div><div class="line"></div><div class="line">            List&lt;Integer&gt; list = <span class="keyword">new</span> ArrayList&lt;&gt;(<span class="number">10</span>);</div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">10</span>; i ++) &#123;</div><div class="line">                list.add(i + <span class="number">1</span>);</div><div class="line">            &#125;</div><div class="line">         System.out.println(<span class="string">"remove之前:"</span> + JSON.toJSONString(list));</div><div class="line"></div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>,size = list.size();i &lt; size;i ++) &#123;</div><div class="line">                <span class="keyword">if</span>(list.get(i) &gt; <span class="number">2</span>) &#123;</div><div class="line">                    list.remove(i);</div><div class="line">                &#125;</div><div class="line">            &#125;</div><div class="line">        System.out.println(<span class="string">"剩余元素:"</span> + JSON.toJSONString(list));</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>我们把list.size()提前存储到size，每次循环比较i &lt; size即可，那么程序运行结果又如何呢？
</code></pre><img src="/typhoon/2018/05/26/list使用中的坑/l3.png" alt="list" title="list">
<pre><code>看到了下表越界异常，为什么，继续看下图:
</code></pre><img src="/typhoon/2018/05/26/list使用中的坑/l4.png" alt="list" title="list">
<pre><code>    那么list中循环移除元素就无解了吗？不，for循环对于遍历List比较擅长性能较好，对于更新操作显得力不从心，那么专业的事情就交给专业的人
去做，这时候迭代器就要大发神威了，再次修改代码如下:
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</div><div class="line"></div><div class="line">            List&lt;Integer&gt; list = <span class="keyword">new</span> ArrayList&lt;&gt;(<span class="number">10</span>);</div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">10</span>; i ++) &#123;</div><div class="line">                list.add(i + <span class="number">1</span>);</div><div class="line">            &#125;</div><div class="line">         System.out.println(<span class="string">"remove之前:"</span> + JSON.toJSONString(list));</div><div class="line"></div><div class="line">            Iterator&lt;Integer&gt; iterator = list.iterator();</div><div class="line">            <span class="keyword">while</span> (iterator.hasNext()) &#123;</div><div class="line">                Integer temp = iterator.next();</div><div class="line">                <span class="keyword">if</span>(temp.intValue() &gt; <span class="number">2</span>) &#123;</div><div class="line">                    iterator.remove();</div><div class="line">                &#125;</div><div class="line">            &#125;</div><div class="line">        System.out.println(<span class="string">"剩余元素:"</span> + JSON.toJSONString(list));</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>运行程序：
</code></pre><img src="/typhoon/2018/05/26/list使用中的坑/l5.png" alt="list" title="list">
<pre><code>可以看到这次我们得到了我们期望的结果，为什么呢？我们看一下list中iterator的实现原理:

1）iterator()方法
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/**</span></div><div class="line"> * Returns an iterator over the elements in this list in proper sequence.</div><div class="line"> *</div><div class="line"> * &lt;p&gt;The returned iterator is &lt;a href="#fail-fast"&gt;&lt;i&gt;fail-fast&lt;/i&gt;&lt;/a&gt;.</div><div class="line"> *</div><div class="line"> * <span class="doctag">@return</span> an iterator over the elements in this list in proper sequence</div><div class="line"> */</div><div class="line"><span class="function"><span class="keyword">public</span> Iterator&lt;E&gt; <span class="title">iterator</span><span class="params">()</span> </span>&#123;</div><div class="line">    <span class="keyword">return</span> <span class="keyword">new</span> Itr();</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>新建了一个Itr对象并返回

2）Itr类
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/**</span></div><div class="line"> * An optimized version of AbstractList.Itr</div><div class="line"> */</div><div class="line"><span class="keyword">private</span> <span class="class"><span class="keyword">class</span> <span class="title">Itr</span> <span class="keyword">implements</span> <span class="title">Iterator</span>&lt;<span class="title">E</span>&gt; </span>&#123;</div><div class="line">    <span class="keyword">int</span> cursor;       <span class="comment">// index of next element to return</span></div><div class="line">    <span class="keyword">int</span> lastRet = -<span class="number">1</span>; <span class="comment">// index of last element returned; -1 if no such</span></div><div class="line">    <span class="keyword">int</span> expectedModCount = modCount;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">hasNext</span><span class="params">()</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> cursor != size;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="meta">@SuppressWarnings</span>(<span class="string">"unchecked"</span>)</div><div class="line">    <span class="function"><span class="keyword">public</span> E <span class="title">next</span><span class="params">()</span> </span>&#123;</div><div class="line">        checkForComodification();</div><div class="line">        <span class="keyword">int</span> i = cursor;</div><div class="line">        <span class="keyword">if</span> (i &gt;= size)</div><div class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> NoSuchElementException();</div><div class="line">        Object[] elementData = ArrayList.<span class="keyword">this</span>.elementData;</div><div class="line">        <span class="keyword">if</span> (i &gt;= elementData.length)</div><div class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> ConcurrentModificationException();</div><div class="line">        cursor = i + <span class="number">1</span>;</div><div class="line">        <span class="keyword">return</span> (E) elementData[lastRet = i];</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">remove</span><span class="params">()</span> </span>&#123;</div><div class="line">        <span class="keyword">if</span> (lastRet &lt; <span class="number">0</span>)</div><div class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> IllegalStateException();</div><div class="line">        checkForComodification();</div><div class="line"></div><div class="line">        <span class="keyword">try</span> &#123;</div><div class="line">            ArrayList.<span class="keyword">this</span>.remove(lastRet);</div><div class="line">            cursor = lastRet;</div><div class="line">            lastRet = -<span class="number">1</span>;</div><div class="line">            expectedModCount = modCount;</div><div class="line">        &#125; <span class="keyword">catch</span> (IndexOutOfBoundsException ex) &#123;</div><div class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> ConcurrentModificationException();</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">final</span> <span class="keyword">void</span> <span class="title">checkForComodification</span><span class="params">()</span> </span>&#123;</div><div class="line">        <span class="keyword">if</span> (modCount != expectedModCount)</div><div class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> ConcurrentModificationException();</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>Itr是ArrayList的一个内部类
有三个重要的方法:
I hasNext()判断当前元素索引是否最后一个
II next()获取当前cursor指向的外层ArrayList数组索引位置
III remove()调用外层ArrayList中的remove方法移除lastRet位置的元素

    为什么Iterator能够安全的移除元素呢？首先remove基本和next()配合使用，也就是说lastRet是实时计算出来的，
这也就避免了下表越界。
</code></pre><h6 id="Arrays-asList-移除元素"><a href="#Arrays-asList-移除元素" class="headerlink" title="Arrays.asList()移除元素"></a>Arrays.asList()移除元素</h6><pre><code>先看一段代码:
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">   <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</div><div class="line">       String str = <span class="string">"1,2,3,4,5"</span>;</div><div class="line">       String[] arr = str.split(<span class="string">","</span>);</div><div class="line">       List&lt;String&gt; list = Arrays.asList(arr);</div><div class="line">       System.out.println(JSON.toJSONString(list));</div><div class="line">       list.remove(<span class="string">"1"</span>);</div><div class="line">System.out.println(JSON.toJSONString(list));</div><div class="line"></div><div class="line">   &#125;</div></pre></td></tr></table></figure>
<pre><code>期望打印结果：[&quot;2&quot;,&quot;3&quot;,&quot;4&quot;,&quot;5&quot;]
运行程序:
</code></pre><img src="/typhoon/2018/05/26/list使用中的坑/l6.png" alt="list" title="list">
<pre><code>程序报了异常，为什么？不多说，这种情况直接看源码，看Arrays.asList实现：
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="keyword">static</span> &lt;T&gt; <span class="function">List&lt;T&gt; <span class="title">asList</span><span class="params">(T... a)</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> <span class="keyword">new</span> ArrayList&lt;&gt;(a);</div><div class="line">    &#125;</div></pre></td></tr></table></figure>
<pre><code>直接新建了一个ArrayList并返回，貌似也没有问题，继续点进去此处ArrayList实现:
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/**</span></div><div class="line"> * <span class="doctag">@serial</span> include</div><div class="line"> */</div><div class="line"><span class="keyword">private</span> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">ArrayList</span>&lt;<span class="title">E</span>&gt; <span class="keyword">extends</span> <span class="title">AbstractList</span>&lt;<span class="title">E</span>&gt;</span></div><div class="line">    <span class="keyword">implements</span> <span class="title">RandomAccess</span>, <span class="title">java</span>.<span class="title">io</span>.<span class="title">Serializable</span></div><div class="line">&#123;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">long</span> serialVersionUID = -<span class="number">2764017481108945198L</span>;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">final</span> E[] a;</div><div class="line"></div><div class="line">    ArrayList(E[] array) &#123;</div><div class="line">        a = Objects.requireNonNull(array);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">size</span><span class="params">()</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> a.length;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> E <span class="title">get</span><span class="params">(<span class="keyword">int</span> index)</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> a[index];</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> E <span class="title">set</span><span class="params">(<span class="keyword">int</span> index, E element)</span> </span>&#123;</div><div class="line">        E oldValue = a[index];</div><div class="line">        a[index] = element;</div><div class="line">        <span class="keyword">return</span> oldValue;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">indexOf</span><span class="params">(Object o)</span> </span>&#123;</div><div class="line">        E[] a = <span class="keyword">this</span>.a;</div><div class="line">        <span class="keyword">if</span> (o == <span class="keyword">null</span>) &#123;</div><div class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; a.length; i++)</div><div class="line">                <span class="keyword">if</span> (a[i] == <span class="keyword">null</span>)</div><div class="line">                    <span class="keyword">return</span> i;</div><div class="line">        &#125; <span class="keyword">else</span> &#123;</div><div class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; a.length; i++)</div><div class="line">                <span class="keyword">if</span> (o.equals(a[i]))</div><div class="line">                    <span class="keyword">return</span> i;</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">return</span> -<span class="number">1</span>;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">contains</span><span class="params">(Object o)</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> indexOf(o) != -<span class="number">1</span>;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>    到这里应该看出了一些端倪，原来这是一个和我们开发常用的ArrayList同名的一个List实现，但是这个ArrayList是Arrays中的一个内部类，
是对List接口的自定义实现，这里的实现没有看到remove方法，类声明中其继承了AbstractList，直接看AbstractList中remove的实现:
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/**</span></div><div class="line">    * &#123;<span class="doctag">@inheritDoc</span>&#125;</div><div class="line">    *</div><div class="line">    * &lt;p&gt;This implementation always throws an</div><div class="line">    * &#123;<span class="doctag">@code</span> UnsupportedOperationException&#125;.</div><div class="line">    *</div><div class="line">    * <span class="doctag">@throws</span> UnsupportedOperationException &#123;<span class="doctag">@inheritDoc</span>&#125;</div><div class="line">    * <span class="doctag">@throws</span> IndexOutOfBoundsException     &#123;<span class="doctag">@inheritDoc</span>&#125;</div><div class="line">    */</div><div class="line">   <span class="function"><span class="keyword">public</span> E <span class="title">remove</span><span class="params">(<span class="keyword">int</span> index)</span> </span>&#123;</div><div class="line">       <span class="keyword">throw</span> <span class="keyword">new</span> UnsupportedOperationException();</div><div class="line">   &#125;</div></pre></td></tr></table></figure>
<pre><code>    我们看到，AbstractList中的remove是个模板方法，如果想在子类对象中使用，那就必须在子类中实现，否则抛异常，到这里我们就明白为什么
上边的代码运行抛异常了。

    那如果把remove改成迭代器中的remove调用，会怎么样呢？把上边的代码稍作修改如下：
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</div><div class="line">    String str = <span class="string">"1,2,3,4,5"</span>;</div><div class="line">    String[] arr = str.split(<span class="string">","</span>);</div><div class="line">    List&lt;String&gt; list = Arrays.asList(arr);</div><div class="line">    System.out.println(JSON.toJSONString(list));</div><div class="line">    Iterator&lt;String&gt; iterator = list.iterator();</div><div class="line">    <span class="keyword">while</span> (iterator.hasNext()) &#123;</div><div class="line">        String temp = iterator.next();</div><div class="line">        <span class="keyword">if</span>(temp.equals(<span class="string">"1"</span>)) &#123;</div><div class="line">            iterator.remove();</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    System.out.println(JSON.toJSONString(list));</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>运行程序：
</code></pre><img src="/typhoon/2018/05/26/list使用中的坑/l7.png" alt="list" title="list">
<pre><code>    程序依旧报了异常，我们继续看源码实现，从上个步骤Arrays的内部类ArrayList源码中我们也没有看到iterator的实现，也就是说也会使用AbstractList
中的实现方式:
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">public</span> Iterator&lt;E&gt; <span class="title">iterator</span><span class="params">()</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> <span class="keyword">new</span> Itr();</div><div class="line">    &#125;</div></pre></td></tr></table></figure>
<pre><code>继续看Itr实现:
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">private</span> <span class="class"><span class="keyword">class</span> <span class="title">Itr</span> <span class="keyword">implements</span> <span class="title">Iterator</span>&lt;<span class="title">E</span>&gt; </span>&#123;</div><div class="line">    <span class="comment">/**</span></div><div class="line">     * Index of element to be returned by subsequent call to next.</div><div class="line">     */</div><div class="line">    <span class="keyword">int</span> cursor = <span class="number">0</span>;</div><div class="line"></div><div class="line">    <span class="comment">/**</span></div><div class="line">     * Index of element returned by most recent call to next or</div><div class="line">     * previous.  Reset to -1 if this element is deleted by a call</div><div class="line">     * to remove.</div><div class="line">     */</div><div class="line">    <span class="keyword">int</span> lastRet = -<span class="number">1</span>;</div><div class="line"></div><div class="line">    <span class="comment">/**</span></div><div class="line">     * The modCount value that the iterator believes that the backing</div><div class="line">     * List should have.  If this expectation is violated, the iterator</div><div class="line">     * has detected concurrent modification.</div><div class="line">     */</div><div class="line">    <span class="keyword">int</span> expectedModCount = modCount;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">hasNext</span><span class="params">()</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> cursor != size();</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> E <span class="title">next</span><span class="params">()</span> </span>&#123;</div><div class="line">        checkForComodification();</div><div class="line">        <span class="keyword">try</span> &#123;</div><div class="line">            <span class="keyword">int</span> i = cursor;</div><div class="line">            E next = get(i);</div><div class="line">            lastRet = i;</div><div class="line">            cursor = i + <span class="number">1</span>;</div><div class="line">            <span class="keyword">return</span> next;</div><div class="line">        &#125; <span class="keyword">catch</span> (IndexOutOfBoundsException e) &#123;</div><div class="line">            checkForComodification();</div><div class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> NoSuchElementException();</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">remove</span><span class="params">()</span> </span>&#123;</div><div class="line">        <span class="keyword">if</span> (lastRet &lt; <span class="number">0</span>)</div><div class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> IllegalStateException();</div><div class="line">        checkForComodification();</div><div class="line"></div><div class="line">        <span class="keyword">try</span> &#123;</div><div class="line">            AbstractList.<span class="keyword">this</span>.remove(lastRet);</div><div class="line">            <span class="keyword">if</span> (lastRet &lt; cursor)</div><div class="line">                cursor--;</div><div class="line">            lastRet = -<span class="number">1</span>;</div><div class="line">            expectedModCount = modCount;</div><div class="line">        &#125; <span class="keyword">catch</span> (IndexOutOfBoundsException e) &#123;</div><div class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> ConcurrentModificationException();</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">final</span> <span class="keyword">void</span> <span class="title">checkForComodification</span><span class="params">()</span> </span>&#123;</div><div class="line">        <span class="keyword">if</span> (modCount != expectedModCount)</div><div class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> ConcurrentModificationException();</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>我们看到Itr是AbstractList中的内部类，重点看一下remove方法，里边最终调用这段逻辑:
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">AbstractList.<span class="keyword">this</span>.remove(lastRet);</div></pre></td></tr></table></figure>
<pre><code>其实是调用AbstractList自己实现的remove方法，回过头来看一下:
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">public</span> E <span class="title">remove</span><span class="params">(<span class="keyword">int</span> index)</span> </span>&#123;</div><div class="line">    <span class="keyword">throw</span> <span class="keyword">new</span> UnsupportedOperationException();</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>    最终调用了和上一步中同样的逻辑，所以出现了和之前同样的异常。对于这种由数组转成List的数据如果
想使用remove移除元素最好转换成标准的ArrayList实现或者自己写逻辑实现。
</code></pre><h6 id="subList子列表问题"><a href="#subList子列表问题" class="headerlink" title="subList子列表问题"></a>subList子列表问题</h6><pre><code>同样，先看一段代码:
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</div><div class="line">      <span class="keyword">int</span> size = <span class="number">5</span>;</div><div class="line">      List&lt;Integer&gt; list = <span class="keyword">new</span> ArrayList&lt;&gt;(<span class="number">5</span>);</div><div class="line">      <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>;i &lt; size; i++) &#123;</div><div class="line">          list.add(i + <span class="number">1</span>);</div><div class="line">      &#125;</div><div class="line">      List&lt;Integer&gt; subList = list.subList(<span class="number">1</span>,list.size());</div><div class="line">        System.out.println(<span class="string">"before父list:"</span> + JSON.toJSONString(list));</div><div class="line">        System.out.println(<span class="string">"before子list:"</span> + JSON.toJSONString(subList));</div><div class="line"></div><div class="line">        subList.add(<span class="number">10</span>);</div><div class="line">        subList.remove(Integer.valueOf(<span class="number">3</span>));</div><div class="line">        System.out.println(<span class="string">"after父list:"</span> + JSON.toJSONString(list));</div><div class="line">        System.out.println(<span class="string">"after子list:"</span> + JSON.toJSONString(subList));</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>这段代码的含义是我先从之前的List构建出一个子list然后对子list进行更新操作，看看对原List有没有影响，
正常情况下，我们业务逻辑中是期望子list操作不影响原list的，运行程序:
</code></pre><img src="/typhoon/2018/05/26/list使用中的坑/l8.png" alt="list" title="list">
<pre><code>运行结果表明我们对子list操作确实影响到了原来的list，为什么呢？翻开源码看实现:
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">public</span> List&lt;E&gt; <span class="title">subList</span><span class="params">(<span class="keyword">int</span> fromIndex, <span class="keyword">int</span> toIndex)</span> </span>&#123;</div><div class="line">       subListRangeCheck(fromIndex, toIndex, size);</div><div class="line">       <span class="keyword">return</span> <span class="keyword">new</span> SubList(<span class="keyword">this</span>, <span class="number">0</span>, fromIndex, toIndex);</div><div class="line">   &#125;</div></pre></td></tr></table></figure>
<pre><code>新建了一个SubList实例并返回，进到SubList中重点看add和remove方法:
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div></pre></td><td class="code"><pre><div class="line">    <span class="keyword">private</span> <span class="class"><span class="keyword">class</span> <span class="title">SubList</span> <span class="keyword">extends</span> <span class="title">AbstractList</span>&lt;<span class="title">E</span>&gt; <span class="keyword">implements</span> <span class="title">RandomAccess</span> </span>&#123;</div><div class="line">        <span class="keyword">private</span> <span class="keyword">final</span> AbstractList&lt;E&gt; parent;</div><div class="line">        <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">int</span> parentOffset;</div><div class="line">        <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">int</span> offset;</div><div class="line">        <span class="keyword">int</span> size;</div><div class="line"></div><div class="line">        SubList(AbstractList&lt;E&gt; parent,</div><div class="line">                <span class="keyword">int</span> offset, <span class="keyword">int</span> fromIndex, <span class="keyword">int</span> toIndex) &#123;</div><div class="line">            <span class="keyword">this</span>.parent = parent;</div><div class="line">            <span class="keyword">this</span>.parentOffset = fromIndex;</div><div class="line">            <span class="keyword">this</span>.offset = offset + fromIndex;</div><div class="line">            <span class="keyword">this</span>.size = toIndex - fromIndex;</div><div class="line">            <span class="keyword">this</span>.modCount = ArrayList.<span class="keyword">this</span>.modCount;</div><div class="line">        &#125;</div><div class="line"></div><div class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">(<span class="keyword">int</span> index, E e)</span> </span>&#123;</div><div class="line">            rangeCheckForAdd(index);</div><div class="line">            checkForComodification();</div><div class="line">            parent.add(parentOffset + index, e);</div><div class="line">            <span class="keyword">this</span>.modCount = parent.modCount;</div><div class="line">            <span class="keyword">this</span>.size++;</div><div class="line">        &#125;</div><div class="line"></div><div class="line">        <span class="function"><span class="keyword">public</span> E <span class="title">remove</span><span class="params">(<span class="keyword">int</span> index)</span> </span>&#123;</div><div class="line">            rangeCheck(index);</div><div class="line">            checkForComodification();</div><div class="line">            E result = parent.remove(parentOffset + index);</div><div class="line">            <span class="keyword">this</span>.modCount = parent.modCount;</div><div class="line">            <span class="keyword">this</span>.size--;</div><div class="line">            <span class="keyword">return</span> result;</div><div class="line">        &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>    从SubList源码中可以看出，创建SubList时将外层ArrayList对象传了进来，也就是说
调用了subList得到和子列表其实是和ArrayList实例指向相同的数据，当然add和remove操作
也是基于相同的数据，这也就导致了对于子列表的更新操作直接影响原列表。
    还有一点值得注意，就是ArrayList调用subList方法的时候，其实是典型的浅复制，换了
 个壳，包的数据没有变。
    既然知道了原理，对于从原列表分割出字列表，并且需要基于字列表操作但是不能影响
 原列表的操作，我们也有相应的实现方案：
 1)重写subList方法，将浅复制改成深复制
 2）逻辑层面不调用subList方法，自己新建对象和列表，将需要操作的数据填充到新对象并
    添加到新列表
</code></pre><h6 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h6><pre><code>    这一篇主要介绍了我们工作中最常用的List使用过程中遇到的一些坑以及其解决方案，
希望给大家在日常开发中带来帮助！
</code></pre><p><strong><em>PS:原创不易,多多支持!</em></strong></p>
<h6 id="附上本人公众号"><a href="#附上本人公众号" class="headerlink" title="附上本人公众号"></a>附上本人公众号</h6><img src="/typhoon/2018/05/26/list使用中的坑/h4.jpg" alt="string" title="string">  
	</div>
		<footer class="article-footer clearfix">
<div class="article-catetags">

<div class="article-categories">
  <span></span>
  <a class="article-category-link" href="/typhoon/categories/java性能优化/">java性能优化</a>
</div>


  <div class="article-tags">
  
  <span></span> <a href="/typhoon/tags/list使用中的坑/">list使用中的坑</a>
  </div>

</div>



	<div class="article-share" id="share">
	
	  <div data-url="http://scorpioaeolus.oschina.io/2018/05/26/list使用中的坑/" data-title="jdk源码分析之List--使用中的坑 | Typhoon&#39;s code space" data-tsina="null" class="share clearfix">
	  </div>
	
	</div>


</footer>

   	       
	</article>
	
<nav class="article-nav clearfix">
 
 <div class="prev" >
 <a href="/typhoon/2018/06/02/dubbo管理平台搭建/" title="dubbo管理平台搭建">
  <strong>上一篇：</strong><br/>
  <span>
  dubbo管理平台搭建</span>
</a>
</div>


<div class="next">
<a href="/typhoon/2018/05/26/jdbcTemplate实现分页/"  title="jdbcTemplate实现物理分页">
 <strong>下一篇：</strong><br/> 
 <span>jdbcTemplate实现物理分页
</span>
</a>
</div>

</nav>

	


</div>  
      <div class="openaside"><a class="navbutton" href="#" title="Show Sidebar"></a></div>

  <div id="toc" class="toc-aside">
  <strong class="toc-title">Contents</strong>
 
 <ol class="toc"><li class="toc-item toc-level-6"><a class="toc-link" href="#循环remove"><span class="toc-number">1.</span> <span class="toc-text">循环remove</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#Arrays-asList-移除元素"><span class="toc-number">2.</span> <span class="toc-text">Arrays.asList()移除元素</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#subList子列表问题"><span class="toc-number">3.</span> <span class="toc-text">subList子列表问题</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#总结"><span class="toc-number">4.</span> <span class="toc-text">总结</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#附上本人公众号"><span class="toc-number">5.</span> <span class="toc-text">附上本人公众号</span></a></li></ol>
 
  </div>

<div id="asidepart">
<div class="closeaside"><a class="closebutton" href="#" title="Hide Sidebar"></a></div>
<aside class="clearfix">

  


  
<div class="categorieslist">
	<p class="asidetitle">Categories</p>
		<ul>
		
		  
			<li><a href="/typhoon/categories/dubbo/" title="dubbo">dubbo<sup>8</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/java性能优化/" title="java性能优化">java性能优化<sup>17</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/java源码分析/" title="java源码分析">java源码分析<sup>1</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/motan/" title="motan">motan<sup>2</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/spring/" title="spring">spring<sup>11</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/springboot/" title="springboot">springboot<sup>28</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/springmvc/" title="springmvc">springmvc<sup>1</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/容器/" title="容器">容器<sup>1</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/工具/" title="工具">工具<sup>7</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/性能优化/" title="性能优化">性能优化<sup>5</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/架构/" title="架构">架构<sup>8</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/案例分析/" title="案例分析">案例分析<sup>13</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/缓存/" title="缓存">缓存<sup>4</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/设计模式/" title="设计模式">设计模式<sup>3</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/面试题/" title="面试题">面试题<sup>1</sup></a></li>
		  
		
		</ul>
</div>


  
<div class="tagslist">
	<p class="asidetitle">Tags</p>
		<ul class="clearfix">
		
			
				<li><a href="/typhoon/tags/springboot/" title="springboot">springboot<sup>28</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/java性能优化/" title="java性能优化">java性能优化<sup>9</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/限流/" title="限流">限流<sup>3</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/Spring5/" title="Spring5">Spring5<sup>3</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/设计模式/" title="设计模式">设计模式<sup>3</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/案例分析/" title="案例分析">案例分析<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/性能优化/" title="性能优化">性能优化<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/Spring/" title="Spring">Spring<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/dubbo/" title="dubbo">dubbo<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/分页/" title="分页">分页<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/并发/" title="并发">并发<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/异常拦截/" title="异常拦截">异常拦截<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/dubbo分布式事务/" title="dubbo分布式事务">dubbo分布式事务<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/自带图片/" title="自带图片">自带图片<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/svn导出mvn项目/" title="svn导出mvn项目">svn导出mvn项目<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/dubbo过滤器/" title="dubbo过滤器">dubbo过滤器<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/排序性能对比/" title="排序性能对比">排序性能对比<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/ListenableFuture/" title="ListenableFuture">ListenableFuture<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/PropertyChangeListener/" title="PropertyChangeListener">PropertyChangeListener<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/jdbcTemplate高可用/" title="jdbcTemplate高可用">jdbcTemplate高可用<sup>1</sup></a></li>
			
		
		</ul>
</div>


  <div class="linkslist">
  <p class="asidetitle">Links</p>
    <ul>
        
          <li>
            
            	<a href="https://coderq.com" target="_blank" title="一个面向程序员交流分享的新一代社区">码农圈</a>
            
          </li>
        
          <li>
            
            	<a href="http://wuchong.me" target="_blank" title="Jark&#39;s Blog">Jark&#39;s Blog</a>
            
          </li>
        
    </ul>
</div>

  


  <div class="rsspart">
	<a href="/atom.xml" target="_blank" title="rss">RSS</a>
</div>

  <div class="weiboshow">
  <p class="asidetitle">Weibo</p>
    <iframe width="100%" height="119" class="share_self"  frameborder="0" scrolling="no" src="http://widget.weibo.com/weiboshow/index.php?language=&width=0&height=119&fansRow=2&ptype=1&speed=0&skin=9&isTitle=1&noborder=1&isWeibo=0&isFans=0&uid=null&verifier=b3593ceb&dpc=1"></iframe>
</div>


</aside>
</div>
    </div>
    <footer><div id="footer" >
	
	<div class="line">
		<span></span>
		<div class="author"></div>
	</div>
	
	
	<section class="info">
		<p> Hello ,I&#39;m a java coder. <br/>
			This is my blog,believe it or not.</p>
	</section>
	 
	<div class="social-font" class="clearfix">
		
		
		
		
		
		
		
		
		
		
		<a href="mailto:ScorpioAeolus@163.com" target="_blank" class="icon-email" title="Email Me"></a>
		
	</div>
			
		

		<p class="copyright">
		Powered by <a href="http://hexo.io" target="_blank" title="hexo">hexo</a> and Theme by <a href="https://github.com/wuchong/jacman" target="_blank" title="Jacman">Jacman</a> © 2019 
		
		<a href="/typhoon/about" target="_blank" title="typhoon">typhoon</a>
		
		
		</p>
</div>
</footer>
    <script src="/typhoon/js/jquery-2.0.3.min.js"></script>
<script src="/typhoon/js/jquery.imagesloaded.min.js"></script>
<script src="/typhoon/js/gallery.js"></script>
<script src="/typhoon/js/jquery.qrcode-0.12.0.min.js"></script>

<script type="text/javascript">
$(document).ready(function(){ 
  $('.navbar').click(function(){
    $('header nav').toggleClass('shownav');
  });
  var myWidth = 0;
  function getSize(){
    if( typeof( window.innerWidth ) == 'number' ) {
      myWidth = window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth) {
      myWidth = document.documentElement.clientWidth;
    };
  };
  var m = $('#main'),
      a = $('#asidepart'),
      c = $('.closeaside'),
      o = $('.openaside');
  c.click(function(){
    a.addClass('fadeOut').css('display', 'none');
    o.css('display', 'block').addClass('fadeIn');
    m.addClass('moveMain');
  });
  o.click(function(){
    o.css('display', 'none').removeClass('beforeFadeIn');
    a.css('display', 'block').removeClass('fadeOut').addClass('fadeIn');      
    m.removeClass('moveMain');
  });
  $(window).scroll(function(){
    o.css("top",Math.max(80,260-$(this).scrollTop()));
  });
  
  $(window).resize(function(){
    getSize(); 
    if (myWidth >= 1024) {
      $('header nav').removeClass('shownav');
    }else{
      m.removeClass('moveMain');
      a.css('display', 'block').removeClass('fadeOut');
      o.css('display', 'none');
      
      $('#toc.toc-aside').css('display', 'none');
        
    }
  });
});
</script>

<script type="text/javascript">
$(document).ready(function(){ 
  var ai = $('.article-content>iframe'),
      ae = $('.article-content>embed'),
      t  = $('#toc'),
      ta = $('#toc.toc-aside'),
      o  = $('.openaside'),
      c  = $('.closeaside');
  if(ai.length>0){
    ai.wrap('<div class="video-container" />');
  };
  if(ae.length>0){
   ae.wrap('<div class="video-container" />');
  };
  c.click(function(){
    ta.css('display', 'block').addClass('fadeIn');
  });
  o.click(function(){
    ta.css('display', 'none');
  });
  $(window).scroll(function(){
    ta.css("top",Math.max(140,320-$(this).scrollTop()));
  });
});
</script>


<script type="text/javascript">
$(document).ready(function(){ 
  var $this = $('.share'),
      url = $this.attr('data-url'),
      encodedUrl = encodeURIComponent(url),
      title = $this.attr('data-title'),
      tsina = $this.attr('data-tsina'),
      description = $this.attr('description');
  var html = [
  '<div class="hoverqrcode clearfix"></div>',
  '<a class="overlay" id="qrcode"></a>',
  '<a href="https://www.facebook.com/sharer.php?u=' + encodedUrl + '" class="article-share-facebook" target="_blank" title="Facebook"></a>',
  '<a href="https://twitter.com/intent/tweet?url=' + encodedUrl + '" class="article-share-twitter" target="_blank" title="Twitter"></a>',
  '<a href="#qrcode" class="article-share-qrcode" title="微信"></a>',
  '<a href="http://widget.renren.com/dialog/share?resourceUrl=' + encodedUrl + '&srcUrl=' + encodedUrl + '&title=' + title +'" class="article-share-renren" target="_blank" title="人人"></a>',
  '<a href="http://service.weibo.com/share/share.php?title='+title+'&url='+encodedUrl +'&ralateUid='+ tsina +'&searchPic=true&style=number' +'" class="article-share-weibo" target="_blank" title="微博"></a>',
  '<span title="Share to"></span>'
  ].join('');
  $this.append(html);

  $('.hoverqrcode').hide();

  var myWidth = 0;
  function updatehoverqrcode(){
    if( typeof( window.innerWidth ) == 'number' ) {
      myWidth = window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth) {
      myWidth = document.documentElement.clientWidth;
    };
    var qrsize = myWidth > 1024 ? 200:100;
    var options = {render: 'image', size: qrsize, fill: '#2ca6cb', text: url, radius: 0.5, quiet: 1};
    var p = $('.article-share-qrcode').position();
    $('.hoverqrcode').empty().css('width', qrsize).css('height', qrsize)
                          .css('left', p.left-qrsize/2+20).css('top', p.top-qrsize-10)
                          .qrcode(options);
  };
  $(window).resize(function(){
    $('.hoverqrcode').hide();
  });
  $('.article-share-qrcode').click(function(){
    updatehoverqrcode();
    $('.hoverqrcode').toggle();
  });
  $('.article-share-qrcode').hover(function(){}, function(){
      $('.hoverqrcode').hide();
  });
});   
</script>









<link rel="stylesheet" href="/typhoon/fancybox/jquery.fancybox.css" media="screen" type="text/css">
<script src="/typhoon/fancybox/jquery.fancybox.pack.js"></script>
<script type="text/javascript">
$(document).ready(function(){ 
  $('.article-content').each(function(i){
    $(this).find('img').each(function(){
      if ($(this).parent().hasClass('fancybox')) return;
      var alt = this.alt;
      if (alt) $(this).after('<span class="caption">' + alt + '</span>');
      $(this).wrap('<a href="' + this.src + '" title="' + alt + '" class="fancybox"></a>');
    });
    $(this).find('.fancybox').each(function(){
      $(this).attr('rel', 'article' + i);
    });
  });
  if($.fancybox){
    $('.fancybox').fancybox();
  }
}); 
</script>



<!-- Analytics Begin -->



<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "//hm.baidu.com/hm.js?e6d1f421bbc9962127a50488f9ed37d1";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>



<!-- Analytics End -->

<!-- Totop Begin -->

	<div id="totop">
	<a title="Back to Top"><img src="/typhoon/img/scrollup.png"/></a>
	</div>
	<script src="/typhoon/js/totop.js"></script>

<!-- Totop End -->

<!-- MathJax Begin -->
<!-- mathjax config similar to math.stackexchange -->


<!-- MathJax End -->

<!-- Tiny_search Begin -->

<!-- Tiny_search End -->

  </body>
</html>
